package com.spdd.mt.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

import static cn.hutool.core.date.DatePattern.PURE_DATETIME_PATTERN;
import static cn.hutool.core.date.DatePattern.PURE_DATE_PATTERN;


public class DateUtils {

    public static String getCurrentMonth() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        return LocalDate.now(ZoneOffset.of("+8")).format(formatter);
    }

    public static String getLastMonth() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        LocalDate localDate = LocalDate.now(ZoneOffset.of("+8"));
        return localDate.minusMonths(1).format(formatter);
    }

    public static String getCurrentDay() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return LocalDate.now(ZoneOffset.of("+8")).format(formatter);
    }

    public static String getCurrentDateTime() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
    }

    public static String getTimestamp() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
    }

    public static String getSpddTimestamp() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        return LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
    }

    /**
     * 当前时间的时间戳（秒）
     *
     * @return 当前时间秒数
     * @since 4.0.0
     */
    public static long currentSeconds() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 根据时间戳生成yyyy-MM-dd HH:mm:ss格式的字符串
     *
     * @param timestamp 时间戳
     */
    public static String getDateTimeByTimestamp(String timestamp) {
        if (StrUtil.isBlank(timestamp)) {
            return "";
        }
        if (!Integer.valueOf(10).equals(StrUtil.length(timestamp))
                && !Integer.valueOf(13).equals(StrUtil.length(timestamp))) {
            return "";
        }
        long longTimestamp = Long.parseLong(timestamp);
        if (Integer.valueOf(10).equals(StrUtil.length(timestamp))) {
            longTimestamp = longTimestamp * 1000;
        }
        Instant instant = Instant.ofEpochMilli(longTimestamp);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 根据时间戳生成yyyy-MM-dd HH:mm:ss格式的字符串
     *
     * @param localDateTime 日期时间
     */
    public static String getDateTimeByDate(LocalDateTime localDateTime) {
        if (Objects.isNull(localDateTime)) {
            return "";
        }
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String convertDateFormat(String inputDate) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = inputFormat.parse(inputDate);

            return outputFormat.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 格式化为 yyyyMMddHHmmss 格式
     *
     * @param date 日期
     */
    public static String formatPureDateTime(Date date) {
        return DateUtil.format(date, PURE_DATETIME_PATTERN);
    }

    /**
     * 格式化为 yyyyMMdd 格式
     *
     * @param date 日期
     */
    public static String formatPureDay(Date date) {
        return DateUtil.format(date, PURE_DATE_PATTERN);
    }

    /**
     * 将日期字符串转换为 Date 对象
     *
     * @param date 日期字符串
     */
    public static Date parseDateTime(String date) {
        try {
            return DateUtil.parse(date);
        } catch (Exception e) {
            return null;
        }
    }
}
